package com.sms.client.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.NoSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.sms.client.ServiceUtil;
import com.sms.shared.SessionInfo;

public class MeterListView extends Composite {
	private Long partyId;
	private String roleName;
	private String menuName;
	private FlexTable mainTable;

	public MeterListView(Long partyId, String roleName, String menuName) {
		this.partyId = partyId;
		this.roleName = roleName;
		this.menuName = menuName;
		mainTable = new FlexTable();
		mainTable.setWidth("100%");
		initWidget(mainTable);
		mainTable.getFlexCellFormatter().setWidth(0, 0, "80%");
		mainTable.getFlexCellFormatter().setWidth(0, 1, "20%");
		mainTable.getFlexCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_TOP);
		initTable();
	}

	public void initTable() {
		SessionInfo userSessionInfo = SessionInfo.getSharedInstance();
		final String sessionId = (String) userSessionInfo.getSessionData(SessionInfo.JSESSION_ID);

		if (roleName.equalsIgnoreCase("Admin"))
			partyId = null;

		ServiceUtil.getMeterServiceAsync().getMapInfo(partyId, new AsyncCallback() {

			@Override
			public void onSuccess(Object result) {
				Map dataMap = (Map) result;
				String mapHeight = (getScreenHeight()*53)/100+"px";
				GoogleMap googleMap = new GoogleMap(menuName,dataMap, "Address", "100%", mapHeight);
				mainTable.setWidget(0, 0, googleMap);
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		});

		ServiceUtil.getMeterServiceAsync().getSiteList(partyId, new AsyncCallback<List<ArrayList>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(List<ArrayList> result) {
				CellTable<ArrayList> table = new CellTable<ArrayList>();
//				table.setStyleName("myCellTableWidget");
				
				TextColumn<ArrayList> nameColumn = new TextColumn<ArrayList>() {
					@Override
					public String getValue(ArrayList arrayList) {
						return (String) arrayList.get(3);
					}
				};

				nameColumn.setSortable(true);

				TextColumn<ArrayList> meterColumn = new TextColumn<ArrayList>() {
					@Override
					public String getValue(ArrayList arrayList) {
						return (String) arrayList.get(1);
					}
				};

				TextColumn<ArrayList> locationColumn = new TextColumn<ArrayList>() {
					@Override
					public String getValue(ArrayList arrayList) {
						return (String) arrayList.get(2);
					}
				};

				table.addColumn(meterColumn, "Meter");
				table.addColumn(locationColumn, "Location");
				table.addColumn(nameColumn, "Name");
				ListDataProvider<ArrayList> dataProvider = new ListDataProvider<ArrayList>();

				dataProvider.addDataDisplay(table);

				List<ArrayList> list = dataProvider.getList();
				for (ArrayList arrayList : result) {
					list.add(arrayList);
				}

				final NoSelectionModel<ArrayList> selectionModel = new NoSelectionModel<ArrayList>();

				selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

					@Override
					public void onSelectionChange(SelectionChangeEvent event) {
						ArrayList selectedRowValues = selectionModel.getLastSelectedObject();
						final Long siteId = (Long) selectedRowValues.get(0);
						final String groupNo = (String) selectedRowValues.get(1);

						ServiceUtil.getUserLoginServiceAsync().setSessionData(groupNo, null,sessionId, new AsyncCallback() {

							@Override
							public void onFailure(Throwable caught) {
								caught.printStackTrace();

							}

							@Override
							public void onSuccess(Object result) {
								if (result == null)
									return;
								Map resultMap = (Map) result;
								SessionInfo mUserSessionInfo = (SessionInfo) resultMap.get("result");
								SessionInfo.setUserSessionInfo(mUserSessionInfo);
								
								String siteId = (String)mUserSessionInfo.getSessionData(SessionInfo.SESSION_SITEID);
								String siteName = (String)mUserSessionInfo.getSessionData(SessionInfo.SESSION_SITENAME);
								RootPanel.get("mainContainer").clear();
								if (menuName.equalsIgnoreCase("Dash Board"))
									RootPanel.get("mainContainer").add(new DashBoardUI(Long.valueOf(siteId),siteName, groupNo, "Daily", "Utility"));
								if (menuName.equalsIgnoreCase("Live"))
									RootPanel.get("mainContainer").add(new LiveProductionUI(Long.valueOf(siteId),siteName, groupNo));

							}
						});
					}
				});
				table.setSelectionModel(selectionModel);
				table.getColumnSortList().push(nameColumn);
				mainTable.setWidget(0, 1, table);

			}
		});

	}

	public static native int getScreenWidth() /*-{
		return $wnd.screen.width;
	}-*/;

	public static native int getScreenHeight() /*-{
		return $wnd.screen.height;
	}-*/;

}
